import {
    sandBox,
    config
} from '../../../lib/myapp.js'
Page({
    data:{
        user:{},
        userId:'',
	    point: ''
    },
    onLoad(e) {
        this.setData({
            userId:!!e.userId ? e.userId :''
        })
        this.getUserDetail();
        this.queryPoint(e.userId);
    },
    getUserDetail(){

        var oauth = wx.getStorageSync('user_token');
        if (!oauth) {
            wx.showModal({
                content:'请先登录',
                success:(res)=>{
                    if (res.confirm) {
                        var route = getCurrentPages()[getCurrentPages().length - 1].route
                        wx.navigateTo({
                            url:`/pages/user/login/login?url=${route}`
                        })
                    }
                }
            })

            return;
        }

        wx.showLoading()
        sandBox.get({
            api:`api/O2O/user/info/${this.data.userId}`,
            header: {Authorization: oauth},
        }).then((res)=>{
            console.log(res)
            var data = res.data
            if (data.status && data.data) {
                this.setData({
                    user:data.data
                })

            } else {
                wx.showModal({
                    content:res.message || '查询会员信息失败',
                    success:(confirm)=>{
                        if (confirm.confirm) {
                            wx.navigateBack();
                        }
                    }
                })
            }

            wx.hideLoading()
        }).catch(()=>{
            wx.showModal({
                content:'查询会员信息失败',
                success:(confirm)=>{
                    if (confirm.confirm) {
                        wx.navigateBack();
                    }
                }
            })
        })
    },
	// 获取用户积分数据
	queryPoint(id) {
		var token = wx.getStorageSync('user_token');

		wx.request({
			url: config.GLOBAL.baseUrl + "api/O2O/user/point/" + id,
			header: {
				Authorization: token
			},
			success: res => {
				if (res.statusCode == 200) {
					res = res.data;
					if (res.status) {
						this.setData({
							point: res.data
						})
					} else {
						wx.showModal({
							title: '',
							content: res.message,
							showCancel: true
						})
					}
				} else {
					wx.showModal({
						title: '',
						content: '获取用户积分失败',
						showCancel: true
					})
				}
			}
		})
	},
    jumpCoupon() {
        wx.navigateTo({
          url: '/pages/member/coupon/coupon?userId=' + this.data.userId
        })
    },
	jumpPoint() {
		wx.navigateTo({
			url: '/pages/member/point/point?userId=' + this.data.userId
		})
	},
	jumpBalance() {
		wx.navigateTo({
			url: '/pages/member/balance/balance?userId=' + this.data.userId
		});

		wx.setStorageSync('select_member', this.data.user);
	}
})